package com.zfdang.touchhelper.tool;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.zfdang.touchhelper.bean.PackagePositionInfoBean;
import com.zfdang.touchhelper.bean.PackageWidgetInfoBean;

import java.lang.reflect.Type;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * Gson库的帮助类
 *
 * @author 黄波
 */
public class GsonUtils {

    public static Map<String, Set<PackageWidgetInfoBean>> getPackageWidgeMaps(String jsonStr) {
        if (jsonStr == null) {
            return new TreeMap<>();
        }

        Gson gson = new Gson();
        Type type = new TypeToken<TreeMap<String, Set<PackageWidgetInfoBean>>>() {
        }.getType();
        return gson.fromJson(jsonStr, type);
    }

    public static Map<String, PackagePositionInfoBean> getPackagePositionMaps(String json) {
        if (json == null) {
            return new TreeMap<>();
        }

        Gson gson = new Gson();
        Type type = new TypeToken<TreeMap<String, PackagePositionInfoBean>>() {
        }.getType();
        return gson.fromJson(json, type);
    }


    /**
     * 对象转换为Json字符串
     */
    public static String toJson(Object obj) {
        if (obj == null) {
            return "传入参数为空，请检查";
        }
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        return gson.toJson(obj);
    }
}
